<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="任务卡编号" prop="takCardCode">
        <el-input v-model="queryParams.takCardCode" placeholder="请输入任务卡编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="overTime">
        <el-date-picker clearable v-model="queryParams.overTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">   制造执行系统
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.mes_wksp_task_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mes:taskcart:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskcartList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="taskCardId" /> -->
      <el-table-column label="任务卡编号" align="center" prop="takCardCode" width="150" />
      <el-table-column label="任务卡进度" align="center" prop="taskCartProductionSchedule" width="250">
        <template slot-scope="scope">
          <el-progress :text-inside="true" :color="customColorMethod(scope.row.taskCartProductionSchedule)"
            :stroke-width="20" :percentage="scope.row.taskCartProductionSchedule" status="exception"></el-progress>
        </template>
      </el-table-column>
      <el-table-column label="车间任务编码" align="center" prop="wkspCode" width="180">
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="proName" width="180">
      </el-table-column>
      <el-table-column label="产品编号" align="center" prop="proCode" width="180">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="tagType(scope.row.status)" effect="dark">
            <dict-tag :options="dict.type.mes_wksp_task_status" :value="scope.row.status" />
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="overTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.overTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status != 2" size="mini" type="text" icon="el-icon-edit" @click="baogong(scope.row)"
            v-hasPermi="['mes:taskcart:edit']">报工</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:taskcart:edit']">修改</el-button> -->
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mes:taskcart:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="报工" :visible.sync="baogongopen" v-if="baogongopen" width="700px" append-to-body >
      <label
        style="background-image: linear-gradient(100deg, #fff4d1 0%, #fda085 100%);padding: 5px;border-radius: 7px;margin-bottom: 10px;display: block;">任务卡编号:
        {{ taskcartList.find(e => e.taskCardId == nowid).takCardCode
        }}</label>
      <label
        style="background-image: linear-gradient(100deg, #c2edff 0%,  #1D8FE1 100%);padding: 5px;border-radius: 7px;margin-bottom: 10px;display: block;">任务卡计划生产数量:
        {{ taskCartDetail.taskCartPlanQuantity }}</label>
      <el-card shadow="never" style="border-radius: 10px;">
        <div>
          <label>选择工序：</label>
          <el-select v-model="proceSeque" filterable placeholder="请选择" @change="proceChange" >
            <el-option v-for="item in proceOptions" :key="item.id" :label="item.proceName" :value="item.proceSeque">
            </el-option>
          </el-select>
          
          <label
            style="background-color: rgb(231, 231, 231);padding: 5px;border-radius: 7px;margin-left: 10px;"><i class="el-icon-warning"></i> 可报工数量:
            {{ TransferredQuantity }}</label>
        </div>

        <div style="margin-top: 10px;">
          <label>报工数量：</label>
          <el-input-number size="mini" v-model="productionQuantity" :precision="0" :min="1"
            :max="TransferredQuantity"></el-input-number>
        </div>
      </el-card>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="report">确 认 报 工</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>





    <!-- 添加或修改车间任务卡对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务卡编号" prop="takCardCode">
          <el-input v-model="form.takCardCode" placeholder="请输入任务卡编号" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="overTime">
          <el-date-picker clearable v-model="form.overTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option v-for="dict in dict.type.mes_wksp_task_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTaskcart, getTaskcart, delTaskcart, addTaskcart, updateTaskcart, sorted, report, getDetail, getTransferredQuantity } from "@/api/mes/taskcart";

export default {
  name: "Taskcart",
  dicts: ['mes_wksp_task_status'],
  data() {
    return {
      //报工工序名称
      productionName:null,
      //报工数量
      productionQuantity: null,
      //工序可报工数量
      TransferredQuantity: null,
      //任务卡详情数据
      taskCartDetail: "",
      //选择报工的工序顺序
      proceSeque: "",
      //当前点击记录id
      nowid: "",
      //工序列表
      proceOptions: [],
      //报工对话框
      baogongopen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车间任务卡表格数据
      taskcartList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        takCardCode: null,
        startTime: null,
        overTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //工序下拉框改变事件
    proceChange(option) {
      getTransferredQuantity({ id: this.nowid, proceSeque: option }).then(res => {
        this.TransferredQuantity = res.data
        this.productionQuantity = this.TransferredQuantity;
      })
      this.productionName=this.proceOptions.find(item=>item.proceSeque==option).proceName
    },
    //标签类型判断
    tagType(status) {
      switch (status) {
        case 1:
          return ""
        case 2:
          return "success"
        case 3:
          return "danger"
      }
    },
    //确认报工
    report() {
      if (this.productionQuantity > this.TransferredQuantity) {
        return this.$modal.msgError("报工数量大于可报工数量");
      }
      const loading = this.$loading({
        lock: true,
        text: '任务卡报工中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      report({ id: this.nowid, proceSeque: this.proceSeque, productionQuantity: this.productionQuantity,proceName:this.productionName }).then(res => {
        this.getList()
        loading.close();
        this.baogongopen = false;
        this.$notify({
          title: '成功',
          message: '报工成功！',
          type: 'success',
          position: 'top-left'
        });
      })
    },
    //报工按钮
    baogong(row) {
      this.baogongopen = true
      this.nowid = row.taskCardId
      this.TransferredQuantity = null;
      this.proceSeque = null;
      sorted({ id: row.taskCardId }).then(res => {
        console.log("🚀 ~ file: index.vue:193 ~ sorted ~ res:", res)
        this.proceOptions = res.data
      })
      getDetail({ id: row.taskCardId }).then(res => {
        this.taskCartDetail = res.data
      })
    },
    //进度条颜色控制
    customColorMethod(data) {
      if (data < 30) {
        return '#f56c6c'
      } else if (data < 100) {
        return '#e6a23c';
      } else {
        return '#67c23a';
      }
    },
    /** 查询车间任务卡列表 */
    getList() {
      this.loading = true;
      listTaskcart(this.queryParams).then(response => {
        this.taskcartList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.baogongopen = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskCardId: null,
        progId: null,
        wkspTaskId: null,
        takCardCode: null,
        taskNeedRptNum: null,
        taskReadyRptNum: null,
        tsakPlanQuantity: null,
        taskReadyQuantity: null,
        startTime: null,
        overTime: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskCardId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车间任务卡";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const taskCardId = row.taskCardId || this.ids
      getTaskcart(taskCardId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车间任务卡";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.taskCardId != null) {
            updateTaskcart(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTaskcart(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskCardIds = row.taskCardId || this.ids;
      this.$modal.confirm('是否确认删除车间任务卡编号为"' + taskCardIds + '"的数据项？').then(function () {
        return delTaskcart(taskCardIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/taskcart/export', {
        ...this.queryParams
      }, `taskcart_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
